package com.logistics.modules.sys.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.NumberUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.logistics.common.utils.PageUtils;
import com.logistics.common.utils.Query;
import com.logistics.modules.sys.dao.FreightcalculaterulesDao;
import com.logistics.modules.sys.entity.FreightcalculaterulesEntity;
import com.logistics.modules.sys.service.FreightcalculaterulesService;
import com.logistics.modules.transportationManagement.entity.CargoEntity;
import com.logistics.modules.transportationManagement.entity.ClientorderEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;


/**
 * impl freightcalculaterules服务
 *
 * @author 水穷处云起时
 * @date 2022/06/01
 */
@Service("freightcalculaterulesService")
public class FreightcalculaterulesServiceImpl extends ServiceImpl<FreightcalculaterulesDao, FreightcalculaterulesEntity> implements FreightcalculaterulesService {
    @Autowired
    private FreightcalculaterulesDao freightcalculaterulesDao;

    /**
     *
     * 多条件组合查询
     * @param params
     * @return
     */
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        //运输规则名称
        String name = Convert.toStr(params.get("name"));
        //货物类型id
        Integer cargoType = Convert.toInt(params.get("cargoType"));
        //货物单位id
        Integer cargoUnit = Convert.toInt(params.get("cargoUnit"));
        //起始地
        String sysStartAddress = Convert.toStr(params.get("sysStartAddress"));
        //目的地
        String sysEndAddress = Convert.toStr(params.get("sysEndAddress"));
        System.out.println(name);
        IPage<FreightcalculaterulesEntity> page = new Query<FreightcalculaterulesEntity>().getPage(params);
        freightcalculaterulesDao.selectAll(page,name,cargoType,cargoUnit,sysStartAddress,sysEndAddress);
        return new PageUtils(page);
    }

    /**
     * 根据货物计算运输成本
     *
     * @param cargo             货物
     * @param clientorderEntity clientorder实体
     * @return {@link Double}
     */
    @Override
    public Double calculateTheShippingCostAccordingToTheGoods(CargoEntity cargo, ClientorderEntity clientorderEntity) {
        // 货物id
        Integer cargoTypeId = cargo.getCargoTypeId();
        // 货物单位
        Integer cargoUnit = cargo.getCargoUnit();
        // 起始地
        String startAddress = clientorderEntity.getStartAddress();
        // 目标地
        String endAddress = clientorderEntity.getEndAddress();

        List<FreightcalculaterulesEntity> rules = freightcalculaterulesDao.selectAllByCustomize(cargoTypeId, cargoUnit, startAddress, endAddress);

        // 判断运费计算规则是否为空
        if (rules == null || rules.size() == 0) {
            throw new RuntimeException("该计划包含的货物有未添加运费计算规则的货物! 入库失败! 请去运费计算规则里添加对应的运费计算规则!");
        }

        // 判断运费计算规则是否为空
        if (rules != null && rules.size() > 1) {
            throw new RuntimeException("入库中包含的货物运费计算规则有两条以上! 请去掉多余的!");
        }
        FreightcalculaterulesEntity rule = rules.get(0);

        // 根据结果计算运费
        BigDecimal div = NumberUtil.div(cargo.getGargoNumber(), rule.getCargoNumber());
        BigDecimal mul = NumberUtil.mul(div, rule.getMoney());
        return mul.doubleValue();
    }

    @Override
    public void insertByOne(FreightcalculaterulesEntity rule) {
        // 校验数据库
        List<FreightcalculaterulesEntity> rules =
                this.baseMapper.selectAllByCustomize(rule.getCargoTypeId(), Convert.toInt(rule.getCargoUnit()), rule.getSysStartAddress(), rule.getSysEndAddress());
        if (rules != null && rules.size() > 0) {
            throw new RuntimeException("添加失败! 您要添加的运费计算规则已存在!");
        }

        this.baseMapper.insert(rule);
    }

}